package com.qiyou.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Formatter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qiyou.dao.IBlogCommentDao;
import com.qiyou.dao.IBlogDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.Blog;
import com.qiyou.entity.BlogComment;

/**
 * 获取博客详细信息
 * 
 */
@WebServlet(name = "BlogInfo", urlPatterns = { "/BlogInfo" })
public class BlogInfoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @SuppressWarnings("resource")
    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String blogid = request.getParameter("id");
        if ((null != blogid) && (blogid.contains("#"))) {
            blogid = blogid.split("#")[0];
        }

        long id = Long.parseLong(blogid);

        IBlogDao blogDao = DaoFactory.getBlogDao();
        IBlogCommentDao blogCommentDao = DaoFactory.getBlogCommentDao();

        Blog result = null;
        List<BlogComment> bcresult = null;

        try {
            result = blogDao.getBlog(id);
            bcresult = blogCommentDao.getBlogComments(id);

            if (null != result) {
                request.setAttribute("blog", result);
                request.setAttribute("year",
                        new Formatter().format("%tY", result.getBlogDate())
                                .toString());
                request.setAttribute("month",
                        new Formatter().format("%tb", result.getBlogDate())
                                .toString());
                request.setAttribute("day",
                        new Formatter().format("%td", result.getBlogDate())
                                .toString());
                request.setAttribute("date",
                        new Formatter().format("%tc", result.getBlogDate())
                                .toString());

                if (null != bcresult) {
                    request.setAttribute("bcresult", bcresult);
                }

                request.getRequestDispatcher("bloginfo.jsp").forward(request,
                        response);

                long num = result.getBlogNums();
                result.setBlogNums(num + 1);
                DaoFactory.getBlogDao().doUpdate(result);
            }
        } catch (SQLException e) {
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
